import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class InitPlaceHolder extends StatefulWidget {
  static const MethodChannel _channel =
      MethodChannel('com.imagepreview.plugin/router_channel');

  const InitPlaceHolder({super.key});

  @override
  _InitialPageState createState() => _InitialPageState();
}

class _InitialPageState extends State<InitPlaceHolder> {
  @override
  void initState() {
    super.initState();
    _getInitialData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(color: Colors.transparent); // 透明占位符
  }

  Future<void> _getInitialData() async {
    try {
      String path = await InitPlaceHolder._channel.invokeMethod('getRoute');
      if (kDebugMode) {
        print('ImagePreview: InitPlaceHolder _getInitialData: $path');
      }
      Future.microtask(() {
        Navigator.pushReplacementNamed(context, path);
      });
    } on PlatformException catch (e) {
      if (kDebugMode) {
        print('ImagePreview: InitPlaceHolder Failed to get data: ${e.message}');
      }
    }
  }
}
